﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Infrastructure.Constants
{
    /// <summary>
    /// 终端类型常量及相关辅助信息
    /// </summary>
    public static class TerminalConstant
    {
        /// <summary>
        /// 电脑端
        /// </summary>
        public const string PC = "PC";

        /// <summary>
        /// 移动端
        /// </summary>
        public const string Mobile = "Mobile";

        /// <summary>
        /// 平板端
        /// </summary>
        public const string Tablet = "Tablet";

        /// <summary>
        /// 其他
        /// </summary>
        public const string Other = "Other";

        /// <summary>
        /// 电脑端常见 User-Agent 关键字
        /// </summary>
        public static readonly string[] PcKeywords =
        [
                "windows", "macintosh", "linux", "x11"
        ];

        /// <summary>
        /// 移动端常见 User-Agent 关键字
        /// </summary>
        public static readonly string[] MobileKeywords =
        [
                "android", "iphone", "ipod", "blackberry", "windows phone", "opera mini", "mobile"
        ];

        /// <summary>
        /// 平板端常见 User-Agent 关键字
        /// </summary>
        public static readonly string[] TabletKeywords =
        [
                "ipad", "tablet"
        ];

        /// <summary>
        /// 判断 User-Agent 字符串对应的终端类型
        /// </summary>
        public static string GetTerminalType(string userAgent)
        {
            if (string.IsNullOrEmpty(userAgent))
                return Other;

            var ua = userAgent.ToLower();

            if (TabletKeywords.Any(k => ua.Contains(k)))
                return Tablet;
            if (MobileKeywords.Any(k => ua.Contains(k)))
                return Mobile;
            if (PcKeywords.Any(k => ua.Contains(k)))
                return PC;
            return Other;
        }
    }
}
